#!/bin/bash
action=$1
target=$2
args=$@

BASE_DIR=/opt
ORIGINAL_PORT=9999
ORIGINAL_VERSION=version
ORIGINAL_ENTRANCE=entrance
ORIGINAL_USERNAME=username
ORIGINAL_PASSWORD=password

function usage() {
    echo "1Panel 控制脚本"
    echo
    echo "Usage: "
    echo "  ./1pctl [COMMAND] [ARGS...]"
    echo "  ./1pctl --help"
    echo
    echo "Commands: "
    echo "  status              查看 1Panel 服务运行状态"
    echo "  start               启动 1Panel 服务"
    echo "  stop                停止 1Panel 服务"
    echo "  restart             重启 1Panel 服务"
    echo "  user-info           获取 1Panel 用户信息"
    echo "  listen-ip           切换 1Panel 监听 IP"
    echo "  version             查看 1Panel 版本信息"
    echo "  update              修改 1Panel 系统信息"
    echo "  reset               重置 1Panel 系统信息"
    echo "  restore             恢复 1Panel 服务及数据"
}
function status() {
    systemctl status 1panel.service
}
function start() {
    systemctl start 1panel.service
    status
}
function stop() {
    systemctl stop 1panel.service
    status
}
function restart() {
    systemctl restart 1panel.service
    status
}
function user-info() {
    1panel user-info
}
function listen-ip() {
    1panel listen-ip
}
function listen_ipv4() {
    1panel listen-ip ipv4
    restart
}
function listen_ipv6() {
    1panel listen-ip ipv6
    restart
}
function version() {
    1panel version
}
function reset() {
    1panel reset
}
function reset_domain() {
    1panel reset domain
}
function reset_entrance() {
    1panel reset entrance
}
function reset_https() {
    1panel reset https
    restart
}
function reset_ips() {
    1panel reset ips
}
function reset_mfa() {
    1panel reset mfa
}
function update() {
    1panel update
}
function update_username() {
    1panel update username
}
function update_password() {
    1panel update password
}
function update_port() {
    1panel update port
}

function main() {
    case "${action}" in
        status)
            status
            ;;
        start)
            start
            ;;
        stop)
            stop
            ;;
        restart)
            restart
            ;;
        user-info)
            user-info
            ;;
        listen-ip)
            case "${target}" in
                ipv4)
                    listen_ipv4
                    ;;
                ipv6)
                    listen_ipv6
                    ;;
                *)
                    listen-ip
                    ;;
            esac
            ;;
        version)
            version
            ;;
        reset)
            case "${target}" in
                domain)
                    reset_domain
                    ;;
                entrance)
                    reset_entrance
                    ;;
                https)
                    reset_https
                    ;;
                ips)
                    reset_ips
                    ;;
                mfa)
                    reset_mfa
                    ;;
                *)
                    reset
                    ;;
            esac
            ;;
        update)
            case "${target}" in
                username)
                    update_username
                    ;;
                password)
                    update_password
                    ;;
                port)
                    update_port
                    ;;
                *)
                    update
                    ;;
            esac
            ;;
        help)
            usage
            ;;
        --help)
            usage
            ;;
        "")
            usage
            ;;
        *)
        echo "不支持的参数，请使用 help 或 --help 参数获取帮助"
    esac
}
main
